package com.iman.sumer.service.impl.space;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.common.Constants;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.support.Page;
import com.iman.sumer.service.space.ShareService;
import com.iman.sumer.vo.EditShareVo;
import com.iman.sumer.vo.sapce.ShareVo;
import com.iman.sumer.vo.sapce.TagVo;

@Service("shareService")
public class ShareServiceImpl implements ShareService {

	@Inject
	TemplateQLService templateQLService;

	public Page<ShareVo> getSharePage(Map<String, Object> map) {
		Integer pageIndex = (Integer) map.get(Constants.PAGE_INDEX_KEY);
		Integer pageSize = (Integer) map.get(Constants.PAGE_SIZE_KEY);
		return templateQLService.findByPagedSQLBeanQuery("get_share_page", map,
				Page.getStartOfPage(pageIndex, pageSize), pageSize, ShareVo.class);
	}

	@Override
	public void insertShared(Map<String, Object> mapResource, Map<String, Object> mapFile) {
		templateQLService.executeSQLUpdate("insert_shared_resource", mapResource);
		templateQLService.executeSQLUpdate("insert_shared_file", mapFile);
	}

	@Override
	public void delSharedResource(Long id,Long userId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		param.put("userId", userId.toString());
		templateQLService.executeSQLUpdate("delete_shared_resource", param);
	}

	@Override
	public void addDownloadCnt(Long id) {
		Map<String, Long> param = new HashMap<String, Long>();
		param.put("id", id);
		templateQLService.executeSQLUpdate("add_download_cnt", param);
	}

	public List<TagVo> getHostTag() {
		return templateQLService.findBySQLBeanQuery("get_host_tag", null, TagVo.class);
	}

	public List<TagVo> getTagList() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("type", "RESOURCE");
		return templateQLService.findBySQLBeanQuery("get_tag_list", param, TagVo.class);
	}

	@Override
	public EditShareVo  getSharedById(Long shareId) {
		Map<String, Long> param = new HashMap<String, Long>();
		param.put("id", shareId);
		return (EditShareVo) templateQLService.findObjectByQuery("get_share_by_id",param,EditShareVo.class);
	}

	@Override
	public void updateShareResource(Map<String,Object> map) {
		templateQLService.executeSQLUpdate("update_share_resource", map);
	}
	


}
